package com.jwthhealth_pub.old.sportfitness.view;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.signutil.model.HomeAdModule;
import com.jwthhealth_pub.old.sportfitness.module.SportHomeModule;
import com.jwthhealth_pub.old.sportfitness.presenter.ISportHomePresenter;
import com.jwthhealth_pub.old.sportfitness.presenter.ISportHomePresenterCompl;
import com.jwthhealth_pub.old.sportfitness.view.adapter.SportFitnessHomeAdapter;
import com.youth.banner.Banner;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerClickListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 运动健身首页
 * Created by Administrator on 2017/1/4.
 */
public class SportFitnessHomeActivity extends Activity {

    public static int SPORT_HOME_AD = 0;
    public static int SPORT_HOME_DATA = 1;

    List<String> mList;

    @BindView(R.id.rb)
    RecyclerView rv;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.progressbar)
    ProgressBar progressbar;
    @BindView(R.id.sign_topbar)
    TitleLayout signTopbar;

    private boolean isRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        setContentView(R.layout.activity_sportfitness);
        ButterKnife.bind(this);

        ISportHomePresenter iSportHomePresenter = new ISportHomePresenterCompl(this);
        if (!isRequest) {
            progressbar();
            iSportHomePresenter.getData();
        }

        initTop();
        initAd();
    }

    private void initAd() {
        Call<HomeAdModule> homeAdModuleCall = ApiHelper.requestSportAd();
        homeAdModuleCall.enqueue(new Callback<HomeAdModule>() {
            @Override
            public void onResponse(Call<HomeAdModule> call, Response<HomeAdModule> response) {
                final HomeAdModule body = response.body();
                if (body.getCode().equals("0")) {
                    initBanner(body.getData());
                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(SportFitnessHomeActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }

            @Override
            public void onFailure(Call<HomeAdModule> call, Throwable t) {
                Log.d("HomeFragment", "fail");
            }
        });
    }

    private void initData(List<SportHomeModule.DataBean> module) {
        GridLayoutManager layout = new GridLayoutManager(this, 2);
        layout.setSmoothScrollbarEnabled(false);

        rv.setLayoutManager(layout);
        rv.setAdapter(new SportFitnessHomeAdapter(this, module));
    }

    private void initBanner(final List<HomeAdModule.DataBean> data) {
        mList = new ArrayList<>();

        for (int i=0; i<data.size(); i++) {
            mList.add(data.get(i).getThumb());
        }


//        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
//        banner.setImageLoader(new MyImageLoader());
//        banner.setImages(mList);
        banner.setBannerAnimation(Transformer.Accordion);
//        banner.isAutoPlay(true);
//        banner.update(mList);
        banner.setDelayTime(5000);
//        banner.startAutoPlay();

        OnBannerClickListener listener = new OnBannerClickListener() {
            @Override
            public void OnBannerClick(int position) {
//                Intent intent = new Intent(SportFitnessHomeActivity.this, AdInfoActivity.class);
//                Bundle bundle = new Bundle();
//                bundle.putSerializable(Constant.ADDATA, data.get(position-1));
//                intent.putExtras(bundle);
//                startActivity(intent);
            }
        };
//        banner.setOnBannerClickListener(listener);
        banner.setImages(mList)
                .setImageLoader(new MyImageLoader())
                .setOnBannerClickListener(listener)
                .start();
    }

    private void initTop() {
        signTopbar.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    public class MyImageLoader extends ImageLoader {

        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            String imgPath = String.valueOf(path);
//            Picasso.with(context).load(imgPath).into(imageView);

            com.jwthhealth_pub.old.common.ImageLoader.picasso(imgPath, imageView);
        }
    }

    private void progressbar() {
        if (progressbar.getVisibility() == View.VISIBLE) {
            progressbar.setVisibility(View.GONE);
            isRequest = false;
        } else {
            progressbar.setVisibility(View.VISIBLE);
            isRequest = true;
        }
    }

    public Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0://ad
                    break;
                case 1://data
                    progressbar();
                    SportHomeModule module = (SportHomeModule) msg.getData().getSerializable(Constant.SPORT_DATA);
                    initData(module.getData());
                    break;
                default:
                    break;
            }
        }
    };

}
